﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace SLSYJ.Controllers.Helpers
{
    public static class HtmlExtensions
    {
        public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression
        )
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var names = Enum.GetNames(metaData.ModelType);
            var sb = new StringBuilder();
            foreach (var name in names)
            {
                var id = string.Format(
                    "{0}_{1}_{2}",
                    htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
                    metaData.PropertyName,
                    name
                );

                var radio = htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString();
                sb.AppendFormat(
                    "<label for=\"{0}\">{1}</label> {2}",
                    id,
                    HttpUtility.HtmlEncode(ChangeName(name)),
                    radio
                );
            }
            return MvcHtmlString.Create(sb.ToString());
        }

        public static MvcHtmlString DropdownForEnum(this System.Web.Mvc.HtmlHelper htmlHelper,
                                  Enum values, string selectedValue, string id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<select name=\"" + id + "\" id=\"" + id.Replace('.','_') + "\">");
            string[] names = Enum.GetNames(values.GetType());
            sb.Append("<option value=\"0\" selected=\"selected\">");
            sb.Append(selectedValue);
            sb.Append("</option>");
            foreach (string name in names)
            {
                sb.Append("<option value=\"");
                sb.Append(((int)Enum.Parse(values.GetType(), name)).ToString());
                sb.Append("\">");
                sb.Append(HttpUtility.HtmlEncode(ChangeBranchName(name)));
                sb.Append("</option>");
            }
            sb.Append("</select>");
            return MvcHtmlString.Create(sb.ToString());
        }

        public static MvcHtmlString ImageFor(this System.Web.Mvc.HtmlHelper helper, string url)
        {
            // Instantiate a UrlHelper 
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            // Create tag builder
            StringBuilder sb = new StringBuilder();
            sb.Append("<img ");

            sb.Append("id=\"DTO_Picture\" name=\"DTO.Picture\" src=\"");
            if (string.IsNullOrEmpty(url))
                sb.Append(urlHelper.Content("~/Content/themes/base/images/facebook-no-image1.gif"));
            else
                sb.Append(urlHelper.Content(url));
            sb.Append("\" alt=\"\" width=\"150px\" height=\"150px\"");
            sb.Append(" />");

            return MvcHtmlString.Create(sb.ToString());
        }

        private static string ChangeName(string name)
        {
            var retval = name;
            switch (name)
            {
                case "Man":
                    retval = "Hombre";
                    break;
                case "Woman":
                    retval = "Mujer";
                    break;
                case "Married":
                    retval = "Casado";
                    break;
                case "Single":
                    retval = "Soltero";
                    break;
                case "Concubinage":
                    retval = "Concuvinato";
                    break;
                default:
                    break;
            }
            return retval;
        }

        private static string ChangeBranchName(string name)
        {
            var retval = name;
            switch (name)
            {
                case "DonTorcuato":
                    retval = "Don Torcuato";
                    break;
                case "SanJuan":
                    retval = "San Juan";
                    break;
                default:
                    break;
            }
            return retval;
        }
    }
}
